import 'package:flutter/material.dart';

import '../../common/const/app_color_const.dart';
import '../../common/const/app_style_const.dart';
import '../../common/type/user_type.dart';
import '../../common/util/common_util.dart';
import '../../common/util/store_util.dart';
import '../../widget/card_container.dart';
import '../../widget/form_item.dart';

class UserInfoPage extends StatefulWidget {
  const UserInfoPage({Key? key}) : super(key: key);

  @override
  State<UserInfoPage> createState() => UserInfoPageState();
}

class UserInfoPageState extends State<UserInfoPage> {
  UserInfoType? userInfo;
  String rtoLiveLocNameLabel = '';

  @override
  void initState() {
    super.initState();
    initData();
  }

  initData() {
    userInfo = StoreUtil.getUserInfo();
  }

  @override
  void dispose() {
    super.dispose();
  }

  buildLiveLocNames(c) {
    List<Widget> list = [];
    if (null != userInfo) {
      for (var element in userInfo!.rtoLiveLocNames!) {
        list.add(Text(
          element,
          style: formItemValueStyle,
        ));
      }
    }

    if (list.isEmpty) {
      list.add(const Text(
        '-',
        style: TextStyle(color: grayColor),
      ));
    }

    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: list,
    );
  }

  userInfoBodyBuilder(context) {
    return Column(
      children: [
        FormItem(
          label: '帐号',
          value: CommonUtil.valueEmptyToDefStr(userInfo?.employeeNum),
        ),
        FormItem(
          label: '姓名',
          value: CommonUtil.valueEmptyToDefStr(userInfo?.userName),
        ),
        FormItem(
          label: '手机号',
          value: CommonUtil.valueEmptyToDefStr(userInfo?.tel),
        ),
        FormItem(
            label: '所属基地',
            crossAxisAlignment: CrossAxisAlignment.start,
            valueBuilder: (c) => buildLiveLocNames(c)),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("个人资料"),
      ),
      body: SingleChildScrollView(
          controller: ScrollController(),
          child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: CardContainer(
                  margin: const EdgeInsets.only(bottom: 10.0),
                  bodyBuilder: (context) => userInfoBodyBuilder(context)))),
    );
  }
}
